package com.atguigu.gmall.common.config;

import com.atguigu.gmall.common.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理类 前提：保证业务模块启动类扫描到
 * 处理web,sevice,mapper抛出各种类型异常，封装标准响应对象Result给客户端
 *
 * @author: atguigu
 * @create: 2023-08-29 09:40
 */
@Slf4j
@RestControllerAdvice  //@ControllerAdvice+@ResponseBody
public class GlobalExceptionHandler {

    /**
     * 处理运行时异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(RuntimeException.class)
    public Result runtimeErr(RuntimeException e) {
        log.error("运行时异常：{}", e);
        return Result.fail().message(e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public Result exceptionErr(Exception e) {
        log.error("Exception异常：{}", e);
        return Result.fail().message(e.getMessage());
    }

    @ExceptionHandler(Throwable.class)
    public Result throwableErr(Throwable e) {
        log.error("Exception异常：{}", e);
        return Result.fail().message(e.getMessage());
    }

}
